package com.virtualCat.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.virtualCat.common.WeChatConfig;
import com.virtualCat.dao.CatDao;
import com.virtualCat.dao.FeedDao;
import com.virtualCat.dao.UserDao;
import com.virtualCat.common.Msg;
import com.virtualCat.entity.Cat;
import com.virtualCat.entity.Feed;
import com.virtualCat.entity.User;
import com.virtualCat.service.UserService;
import com.virtualCat.vo.UserInfoDetailVo;
import com.virtualCat.vo.UserInfoSimpleVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private FeedDao feedDao;
    @Autowired
    private CatDao catDao;

    /**
     * 修改地址
     * @param openid
     * @param receiver
     * @param phone
     * @param area
     * @param build_number
     * @return
     */
    public Msg updateAddress(String openid, String receiver,String phone,String area,String build_number) {
        int resultCount = userDao.selectUserByOpenid(openid);
        if (resultCount < 0){
            return Msg.fail().add("用户不存在","10901");
        }
        if (StringUtils.isNotBlank(receiver) && StringUtils.isNotBlank(phone) && StringUtils.isNotBlank(area) && StringUtils.isNotBlank(build_number)){
            User user=userDao.findByOpenid(openid);
            user.setReceiver(receiver);
            user.setPhone(phone);
            user.setArea(area);
            user.setBuild_number(build_number);
            int result=userDao.updateUser(user);
            if (result>0){
                return Msg.success();
            }
            return Msg.fail().add("更改失败","10902");
        }
        return Msg.fail().add("输入的数据不能为空","10903");
    }

    //==============================后台===================================//

    /**
     * 获取用户的信息
     * @return
     */
    public Msg getUserInfo(Integer pageNum,Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<User> users = userDao.findAll();
        if (users == null || users.size() == 0){
            return Msg.fail().add("无数据","not data");
        }
        List<UserInfoSimpleVo> userInfoSimpleVos = new ArrayList<UserInfoSimpleVo>();
        for(User user:users){
            UserInfoSimpleVo userInfoVo = new UserInfoSimpleVo();
            userInfoVo.setOpenid(user.getOpenid());
            userInfoVo.setNickName(user.getNickName());
            userInfoVo.setImgUrl(user.getImgUrl());
            userInfoVo.setSex(user.getSex());
            userInfoVo.setHistory(user.getHistory());
            userInfoVo.setObtain(user.getObtain());
            Feed feed = feedDao.findByOpenid(user.getOpenid());
            if (feed != null){
                Cat cat =  catDao.findById(feed.getCatid());
                if (cat != null){
                    userInfoVo.setCat_name(cat.getCname());
                    userInfoVo.setCimg(WeChatConfig.PIC_RESOURCES_PREFIX_PATH+cat.getCimg());
                }
            }
            userInfoSimpleVos.add(userInfoVo);
            userInfoVo = null;
        }
        PageInfo pageInfo = new PageInfo(userInfoSimpleVos);
        int userCount = userDao.countUser();
        return Msg.success().add("userCount",userCount).add("userInfos",pageInfo);
    }

    /**
     * 获取单个用户信息
     * @param openid
     * @return
     */
    public Msg getUserInfoById(String openid){
        User user = userDao.findByOpenid(openid);
        if (user == null){
            return Msg.fail().add("fail","not found");
        }
        UserInfoDetailVo userInfoVo = new UserInfoDetailVo();
        userInfoVo.setOpenid(user.getOpenid());
        userInfoVo.setNickName(user.getNickName());
        userInfoVo.setImgUrl(user.getImgUrl());
        userInfoVo.setSex(user.getSex());
        userInfoVo.setHistory(user.getHistory());
        userInfoVo.setCfood(user.getCat_food());
        userInfoVo.setObtain(user.getObtain());
        Feed feed = feedDao.findByOpenid(openid);
        if (feed != null){
            userInfoVo.setFeedCount(feed.getFeedCount());
            userInfoVo.setFeedTime(feed.getFeedTime());
            userInfoVo.setShareCount(feed.getShareCount());
            userInfoVo.setDiscover(feed.getDiscover());
            userInfoVo.setGrowth(feed.getGrowth());
            Cat cat =  catDao.findById(feed.getCatid());
            if (cat != null){
                userInfoVo.setCatid(cat.getCatid());
                userInfoVo.setCname(cat.getCname());
                userInfoVo.setCimg(WeChatConfig.PIC_RESOURCES_PREFIX_PATH+cat.getCimg());
            }
        }
        return Msg.success().add("userInfo",userInfoVo);
    }


    public Msg updateUserInfo(UserInfoDetailVo info) {
        int rowCount;
        if (info.getHistory() != null){
            rowCount = userDao.updateFoodAndHistory(info.getCfood(),info.getHistory(),info.getOpenid());
            if (rowCount == 0){
                return Msg.fail();
            }
        }
        Feed feed = feedDao.findByOpenid(info.getOpenid());
        feed.setGrowth(info.getGrowth());
        feed.setShareCount(info.getShareCount());
        feed.setFeedCount(info.getFeedCount());
        feed.setDiscover(info.getDiscover());
        rowCount = feedDao.updateFeed(feed);
        if (rowCount == 0){
            return Msg.fail();
        }
        return Msg.success();
    }


    public Msg deleteUser(String openid){
        int rowCount = feedDao.deleteFeed(openid);
        if (rowCount < 0){
            return Msg.fail().add("fail","delete fail");
        }
        rowCount = userDao.deleteUser(openid);
        if (rowCount < 0){
            return Msg.fail().add("fail","delete fail");
        }
        return Msg.success();
    }

}
